package com.example.blogsystem.controller;

import com.example.blogsystem.model.BlogInfo;
import com.example.blogsystem.service.BlogService;
import com.example.blogsystem.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    //获取博客返回前端
    @RequestMapping("/queryBlogList")
    public List<BlogInfo> queryBlog() {
        List<BlogInfo> bloglist = blogService.queryBlog();
        return bloglist;
    }
    //获取博客详情页面
    @RequestMapping("/queryBlogDetail")
    public BlogInfo queryBlogDetail(Integer blogId,HttpServletRequest request) {
        if(blogId == null) {
            throw new UnsupportedOperationException("blogId不能为null");
        }
        BlogInfo blogInfo = blogService.queryBlogDetail(blogId);
        //拿到作者ID
        Integer authorId = blogInfo.getUserId();
        //登录用户,从token中获取
        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserInfoToken(token);
        if(authorId != null && authorId == userId) {
            blogInfo.setLoginUser(true);
        }
        return blogInfo;
    }
    /*
    * 发布博客
    * */
    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        Assert.notNull(blogInfo.getTitle(),"标题不能为空");
        Assert.notNull(blogInfo.getContent(),"内容不能为空");
        //获取token
        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserInfoToken(token);
        Assert.notNull(userId,"用户未登录");
        blogInfo.setUserId(userId);
        Integer result = null;
        try {
            result = blogService.insertBlog(blogInfo);
            if(result == 1) {
                return true;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /*
    * 更新博客
    * */
    @RequestMapping("/update")
    public Boolean updateBlog(@RequestBody BlogInfo blogInfo) {
        Assert.notNull(blogInfo.getId(),"id不能为空");
        Integer result = null;
        try {
            result = blogService.update(blogInfo);
            if(result >= 1) {
                return true;
            }
        }catch (Exception e) {
            log.error("updateBlog发生异常，e：",e);
        }
        return false;

    }
    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId) {
        Assert.notNull(blogId,"删除的参数不能为null");
        Integer result = null;
        try {
            BlogInfo blogInfo = new BlogInfo();
            blogInfo.setId(blogId);
            blogInfo.setDeleteFlag(1);
            result = blogService.update(blogInfo);
            if(result >= 1) {
                return true;
            }
        }catch (Exception e) {
            log.error("updateBlog发生异常，e：",e);
        }
        return false;
    }
}
